<template>
  <div>
    <div style="display: flex; justify-content: center; margin-top: 10px">
      <el-input v-model="keywords" placeholder="通过用户名搜索" prefix-icon="el-icon-search" style="width: 500px; margin-right: 10px"></el-input>
      <el-button type="primary" icon="el-icon-search" @click="doSearch">搜索</el-button>
    </div>
    <div class="admin-container">
      <el-card class="box-card" v-for="(admin, index) in admins" :key="index">
        <div slot="header" class="clearfix">
          <span>{{ admin.name }}</span>
          <el-button type="danger" style="float: right;" size="mini" icon="el-icon-delete" @click="deleteAdmin(admin)">删除</el-button>
        </div>
        <div style="display: flex">
          <div style="margin-right: 20px;">
            <img :src="admin.userFace" :alt="admin.name" title="admin.name" class="userface">
          </div>
          <div>
            <div>用户名：{{ admin.name }}</div>
            <div>手机号：{{ admin.phone }}</div>
            <div>电话号：{{ admin.telephone }}</div>
            <div>地址：{{ admin.address }}</div>
            <div>用户状态：
              <el-switch
              v-model="admin.enabled"
              active-color="#13ce66"
              inactive-color="#ff4949"
              @change="enabledChange(admin)"
              active-text="启用"
              inactive-text="禁用">
              </el-switch>
            </div>
            <div>用户角色：
              <el-tag style="margin-right: 4%" type="success" v-for="(role, indexj) in admin.roles" :key="indexj">
                {{role.nameZh}}
              </el-tag>
              <el-popover
                placement="bottom"
                title="角色列表"
                @show="showPop(admin)"
                width="200px"
                @hide="hidePop(admin)"
                trigger="click">
                <el-select v-model="selectedRoles" multiple placeholder="请选择">
                  <el-option
                  v-for="(r, index) in allRoles"
                  :key="index"
                  :label="r.nameZh"
                  :value="r.id">
                  </el-option>
                </el-select>
                  <el-button slot="reference" type="text" icon="el-icon-more"></el-button>
              </el-popover>
            </div>
            <div>
              备注：{{ admin.remark }}
            </div>
        </div>
        </div>
      </el-card>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      admins: [],
      keywords: '',
      allRoles: [],
      selectedRoles: []
    }
  },
  mounted () {
    this.initAdmins()
  },
  methods: {
    hidePop (admin) {
      let roles = []
      Object.assign(roles, admin.roles)
      let flag = false;
      if (roles.length != this.selectedRoles.length) {
        flag = true
      } else {
        for (let i =1; i<roles.length; i++) {
          let role = roles[i]
          for (let j = 0; i < this.selectedRoles.length; i++) {
            let sr = this.selectedRoles[j]
            if (role.id == sr) {
              roles.splice(i,1)
              i--
              break
            }
          }
        }
        if( roles.length == 0) {
          flag = true
        }
      }
      if(flag) {
        let url = '/system/admin/role?adminId=' + admin.id
        this.selectedRoles.forEach(sr => {
          url += '&rids=' + sr
        })
        this.putRequest(url).then(resp => {
          if (resp) {
            this.initAdmins()
          }
        })
      }
    },
    showPop (admin) {
      this.initAllRoles()
      let roles = admin.roles
      this.selectedRoles = []
      roles.forEach(r => {
        this.selectedRoles.push(r.id)
      });
    },
    initAllRoles () {
      this.getRequest('/system/admin/roles').then(resp => {
        if (resp) {
          this.allRoles = resp.data
        }
      })
    },
    enabledChange (admin) {
      this.putRequest('/system/admin/', admin).then(resp => {
        if (resp) {
          this.initAdmins()
        }
      })
    },
    deleteAdmin (admin) {
      console.log(admin)
      this.deleteRequest('/system/admin/?id=' + admin.id).then( resp => {
        if (resp) {
          this.initAdmins()
        }
      })
    },
    doSearch () {
      this.initAdmins()
    },
     initAdmins () {
       this.getRequest('/system/admin/?keywords=' + this.keywords).then(resp => {
         if (resp) {
           this.admins = resp.data
           console.log("admins")
           console.log(this.admins)
         }
       })
     }
  }
}
</script>

<style>
.admin-container {
  display: flex;
  margin-top: 20px;
  flex-wrap: wrap;
}
.box-card {
  width: 500px;
}
.userface {
  width: 100px;
  height: 100px;
  border-radius: 100px;
  padding-top: -50%
}
</style>
